package www.houdoudou.com.supplier;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import www.houdoudou.com.common.CommonFunction;

public class BranchManagement {

	public static JSONObject manage(String jsonData){
		JSONObject result = new JSONObject();
		JSONObject jsonfrom= JSONObject.fromObject(jsonData);				
		//判断请求的功能
		int FunctionCode=jsonfrom.getInt("BranchFunctionCode");
		   PreparedStatement pstm=null; 
		   Statement  stm=null;
		   ResultSet rs=null; 		 
		   Connection conn= CommonFunction. getCurrentConnection();
		   // 此值代表用户身份，不同的身份，对于同一个增删改查函数，有不同的处理，防止出错。
		   int Manager_Authority= jsonfrom.getInt("Manager_Authority");
		   switch(FunctionCode){
		   
		   
		   
		   
		   
			case 3:{
			     // 只允许商家删 
							if(Manager_Authority==5){
								// 这里还必须做一个判断 判断是否有产品
						
								 int  Product_ID=jsonfrom.getInt("Product_ID");
								
								 try{													  
									 String	sql	=" delete from  Tb_Branch where Product_ID="+Product_ID+"";									   
									             stm=conn.createStatement(ResultSet.TYPE_FORWARD_ONLY,ResultSet.CONCUR_UPDATABLE);  							           
									            		 stm.executeUpdate(sql);  	
									     				
															result.put("ActionStatus", true);
															result.put("ErrorInfo", "删除子配成功");
												} catch (SQLException e) {
													// TODO Auto-generated catch block
													e.printStackTrace();
													result.put("ActionStatus", false);
													result.put("ErrorInfo", "数据库删除操作失败子配");
												    break;
												} 							
										
								
									
						}else{
							result.put("ActionStatus", false);
							result.put("ErrorInfo", "无权操作");}
						
				
				
			break;}// case 3
		   // 新增
					case 4:{
						 if(Manager_Authority==5){
								// step2 检查是否已经存在
							 JSONArray Branches= jsonfrom.getJSONArray("Branches");
							 JSONArray BackBranches=new JSONArray();
							 for(int i=0;i<Branches.size();i++){
							 try{		
								 JSONObject  backtemp=new JSONObject();
								  String sql = "insert  into Tb_Branch (Branch_Name,Branch_UnitPrice,Branch_isAuthority,Product_ID,Branch_Weight) values (?,?,?,?,?)";
					              	pstm=conn.prepareStatement(sql,Statement.RETURN_GENERATED_KEYS);
									pstm.setString(1, Branches.getJSONObject(i).getString("Branch_Name"));
									pstm.setDouble(2, Branches.getJSONObject(i).getDouble("Branch_UnitPrice"));
									pstm.setString(3, Branches.getJSONObject(i).getString("Branch_isAuthority"));
									pstm.setInt(4, jsonfrom.getInt("Product_ID"));
									pstm.setInt(5, Branches.getJSONObject(i).getInt("Branch_Weight"));											
								    pstm.executeUpdate();
								    result.put("ActionStatus", true);
									result.put("ErrorInfo", "子配增加成功");
									rs=pstm.getGeneratedKeys();
									if(rs.next()){
										backtemp.put("Branch_ID", rs.getInt(1));
										BackBranches.add(backtemp);
									}else{
										 result.put("ActionStatus", false);
											result.put("ErrorInfo", "获取子配自增编号失败");
									}
								
							 } catch (SQLException e) {
									// TODO Auto-generated catch block
									e.printStackTrace();
									result.put("ActionStatus", false);
									result.put("ErrorInfo", "子配添加数据库异常");
									break;
								}
							 }// end of for
							 
							 result.put("Branches", BackBranches);
							   	
								 }// end of if manager_authority==4
								 else{
									 result.put("ActionStatus", false);
										result.put("ErrorInfo", "你没有权限添加子配");}			
					   break;}// case 4
					
					//查询所有 必须带着供应商和产品ID的参数来查，不然没有意义
					 case 5:{
						  try { 
							  String sql="";
							  
							    int  Product_ID=jsonfrom.getInt("Product_ID");
							  
								   int Query_Way=jsonfrom.getInt("Query_Way");
									switch(Query_Way){
									case 2:{   
										  
									           sql = "select * from Tb_Branch where Product_ID="+Product_ID+" and Branch_isAuthority="+"1"+""; 
									           System.out.println(sql);
									           break;}
									case 1:{   
									    
										sql = "select * from  Tb_Branch where Product_ID="+Product_ID+"";
								                   break;}
							                   }
							                    
							   
								stm=conn.createStatement(ResultSet.TYPE_FORWARD_ONLY,ResultSet.CONCUR_READ_ONLY);
								rs= stm.executeQuery(sql);
								JSONArray Branches= new JSONArray();
								
								while(rs.next()){
									JSONObject branch= new JSONObject();
									boolean isadd=false;
								switch(Manager_Authority){							            
								case 5:{					
										isadd=true;
									        break;}	
								case 6:{					
									isadd=true;
								        break;}	
					              }
								if(isadd){							
									branch.put("Branch_ID", rs.getInt("Branch_ID"));
									branch.put("Branch_Weight", rs.getInt("Branch_Weight"));
									branch.put("Branch_isAuthority", rs.getString("Branch_isAuthority"));
									branch.put("Branch_Name", rs.getString("Branch_Name"));
									branch.put("Branch_UnitPrice", rs.getDouble("Branch_UnitPrice"));
									
			                        Branches.add(branch);               
								                         }
								
								}
								
								result.put("ActionStatus", true);
								result.put("ErrorInfo", "成功");
								result.put("Branches",Branches);
								
						  }catch (SQLException e) {
								// TODO Auto-generated catch block
								e.printStackTrace();
								result.put("ActionStatus", false);
								result.put("ErrorInfo", "查询失败");
							} 
								
								
							 break;  } //case5
		   
		   }		   
		   // 不能关闭连接，由上一级调用来关。
		   CommonFunction.closeConn(null, pstm, stm, rs);
		   return result;
		   }
	
	
}
